/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.rukopisy.tools.cli;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class Main {

    private static String ACTION_BUILD_ITEM_TRANSFERS = "buildItemTransfers";
    private static String ACTION_ADD_K4_PIDS = "addK4Pids";
    private static String ACTION_IMPORT_MISSING_FOXMLS = "importFoxmls";
    private static String ACTION_UPDATE_MODELS = "updateModels";
    private static String ACTION_UPDATE_LINKS = "updateLinks";
    private static String OPT_ACTION = "action";
    private static String OPT_PROPERTIES = "properties";
    private static String OPT_MAPPER = "mapper";
    private static String OPT_RESULT_DIR = "resultDir";
    private static String OPT_TEI_DIR = "teiDir";
    private static String OPT_REP_URL = "repositoryUrl";
    private static String OPT_FILES_TO_IMPORT_FILE = "filesList";
    private static String OPT_PID_LIST = "pidList";
    private static String OPT_MODEL = "model";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String[] testArgs = new String[]{
            //            "-action", ACTION_ADD_K4_PIDS,
            //            "-teiDir", "/home/rehan/rukopisy/ENRICH",
            //            "-repositoryUrl", "http://kramerius4.mzk.cz"

            //            "-action", "OPT_BUILD_ITEM_TRANSFERS",
            //            "-properties", "/home/rehan/NetBeansProjects/repositoryManager/local.properties",
            //            "-resultDir", "/home/rehan/rukopisy/moveItems",
            //            "-mapper", "/home/rehan/rukopisy/mapping.xml",
            //            "-teiDir", "/home/rehan/rukopisy/ENRICH"

            //            "-action", ACTION_IMPORT_MISSING_FOXMLS,
            //            "-properties", "/home/rehan/properties/kostry.properties",
            //            "-filesList", "/home/rehan/rukopisy/filesToIngest.head"

            //            "-action", ACTION_UPDATE_MODELS,
            //            "-properties", "/home/rehan/properties/kostry.properties",
            //            "-model", "manuscript",
            //            "-pidList", "/home/rehan/tmp/RkpRkp2ChPids.txt"

            //            "-action", ACTION_UPDATE_LINKS,
            //            "-properties", "/home/rehan/rukopisy/updateLinks.properties",
            //            "-pidList", "/home/rehan/tmp/monographsToImageServer.txt",
            //            //"-teiDir", "/home/rehan/rukopisy/ENRICH/Ks"
            //            "-teiDir", "/home/rehan/rukopisy/ENRICH/Ks"

            "-action", ACTION_UPDATE_LINKS,
            "-properties", "/home/rehan/rukopisy/updateLinksOstry.properties",
            "-pidList", "/home/rehan/tmp/monographsToImageServer.txt",
            "-teiDir", "/home/rehan/rukopisy/ENRICH/Ks"
        };
        //args = testArgs;
        try {
            Action action = determineAction(args);
            action.run();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static Action determineAction(String[] args) throws Exception {
        CommandLine cmd = loadCommandLine(args);
        String action = cmd.getOptionValue(OPT_ACTION);
        if (ACTION_BUILD_ITEM_TRANSFERS.equals(action)) {
            return getBuildItemTransferAction(cmd);
        }
        if (ACTION_ADD_K4_PIDS.equals(action)) {
            return getAddK4PidsAction(cmd);
        }
        if (ACTION_IMPORT_MISSING_FOXMLS.equals(action)) {
            return getImportFoxmlsAction(cmd);
        }
        if (ACTION_UPDATE_MODELS.equals(action)) {
            return getUpdateModelsAction(cmd);
        }
        if (ACTION_UPDATE_LINKS.equals(action)) {
            return getLinkToImageServerAction(cmd);
        }
        throw new Exception("no such action '" + action + "'");
    }

    private static CommandLine loadCommandLine(String[] args) throws ParseException {
        Options options = new Options();
        Option action = new Option(OPT_ACTION, "", true, "");
        action.setRequired(true);

        Option propertiesFile = new Option(OPT_PROPERTIES, "", true, "");
        propertiesFile.setRequired(false);
        options.addOption(propertiesFile);

        Option mapper = new Option(OPT_MAPPER, "", true, "");
        mapper.setRequired(false);
        options.addOption(mapper);

        Option resultDir = new Option(OPT_RESULT_DIR, "", true, "");
        resultDir.setRequired(false);
        options.addOption(resultDir);

        Option teiDir = new Option(OPT_TEI_DIR, "", true, "");
        teiDir.setRequired(false);
        options.addOption(teiDir);

        Option repositoryUrl = new Option(OPT_REP_URL, "", true, "");
        repositoryUrl.setRequired(false);
        options.addOption(repositoryUrl);

        Option filesToImport = new Option(OPT_FILES_TO_IMPORT_FILE, "", true, "");
        filesToImport.setRequired(false);
        options.addOption(filesToImport);

        Option pidList = new Option(OPT_PID_LIST, "", true, "");
        pidList.setRequired(false);
        options.addOption(pidList);

        Option model = new Option(OPT_MODEL, "", true, "");
        model.setRequired(false);
        options.addOption(model);

        options.addOption(action);
        CommandLineParser parser = new PosixParser();
        return parser.parse(options, args);
    }

    private static Action getAddK4PidsAction(CommandLine cmd) throws Exception {
        String teiDir = loadOption(cmd, OPT_TEI_DIR);
        String repositoryUrl = loadOption(cmd, OPT_REP_URL);
        return new AddK4PidsAction(new File(teiDir), repositoryUrl);
    }

    private static Action getBuildItemTransferAction(CommandLine cmd) throws Exception {
        String properties = loadOption(cmd, OPT_PROPERTIES);
        String mapper = loadOption(cmd, OPT_MAPPER);
        String resultDir = loadOption(cmd, OPT_RESULT_DIR);
        String teiDir = loadOption(cmd, OPT_TEI_DIR);
        return new BuildItemTransfersAction(properties, mapper, resultDir, teiDir);
    }

    private static Action getImportFoxmlsAction(CommandLine cmd) throws Exception {
        File properties = new File(loadOption(cmd, OPT_PROPERTIES));
        File foxmlFilesList = new File(loadOption(cmd, OPT_FILES_TO_IMPORT_FILE));
        return new ImportFoxmlsAction(properties, foxmlFilesList);
    }

    private static Action getUpdateModelsAction(CommandLine cmd) throws Exception {
        File properties = new File(loadOption(cmd, OPT_PROPERTIES));
        File pidList = new File(loadOption(cmd, OPT_PID_LIST));
        String model = loadOption(cmd, OPT_MODEL);
        return new UpdateModelsAction(properties, pidList, model);
    }

    private static Action getLinkToImageServerAction(CommandLine cmd) throws Exception {
        File properties = new File(loadOption(cmd, OPT_PROPERTIES));
        File pidList = new File(loadOption(cmd, OPT_PID_LIST));
        File teiDir = new File(loadOption(cmd, OPT_TEI_DIR));
        return new LinkToImageServerAction(properties, pidList, teiDir);
    }

    private static String loadOption(CommandLine cmd, String option) throws Exception {
        String value = cmd.getOptionValue(option);
        if (value == null) {
            throw new Exception("missing option " + option);
        }
        return value;
    }
}
